package com.jhinwins.equipmenteb.equipmentebserver.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jhinwins.equipmenteb.equipmentebserver.dao.OrderDao;
import com.jhinwins.equipmenteb.equipmentebserver.entity.OrderBean;
import com.jhinwins.equipmenteb.equipmentebserver.entity.ProductBean;
import com.jhinwins.equipmenteb.equipmentebserver.entity.SpecValueBean;
import com.jhinwins.equipmenteb.equipmentebserver.entity.UserBean;
import com.jhinwins.equipmenteb.equipmentebserver.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    private OrderDao orderDao;

    @Autowired
    public OrderServiceImpl(OrderDao orderDao) {
        this.orderDao = orderDao;
    }

    @Override
    public JSONArray getOrders(Long userid, String orderState) {
        JSONArray datas = new JSONArray();

        OrderBean orderExa = new OrderBean();

        if (orderState != null && orderState.length() > 0)
            orderExa.setState(orderState);

        if (userid != null && userid > 0) {
            UserBean userExa = new UserBean();
            userExa.setId(userid);
            orderExa.setUserBean(userExa);
        }

        //通过检测state字段来判断是查询购物车还是订单
        ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("state", ExampleMatcher.GenericPropertyMatchers.startsWith());

        Example<OrderBean> example = Example.of(orderExa, matcher);
        List<OrderBean> orderBeans = orderDao.findAll(example);

        for (OrderBean orderBean : orderBeans) {
            ProductBean productBean = orderBean.getInventoryBean().getProductBean();
            JSONObject orderItem = new JSONObject();
            orderItem.put("name", productBean.getName());
            orderItem.put("imgUrl", productBean.getProductImgBeans().size() > 0 ? productBean.getProductImgBeans().get(0).getImgUrl() : "");
            orderItem.put("tenancy", orderBean.getTenancy());
            orderItem.put("count", orderBean.getCount());
            orderItem.put("price", orderBean.getInventoryBean().getPrice());
            orderItem.put("id", orderBean.getId());
            orderItem.put("state", orderBean.getState());
            orderItem.put("user", orderBean.getUserBean());
            orderItem.put("revertTime", orderBean.getTime());
            StringBuilder specification_str = new StringBuilder();
            for (SpecValueBean specValueBean : orderBean.getInventoryBean().getSpecCfg().values()) {
                specification_str.append(specValueBean.getName());
                specification_str.append(",");
            }
            orderItem.put("specification", specification_str.toString());
            datas.add(orderItem);
        }

        return datas;
    }

    @Override
    public JSONObject settlementByIds(List<Long> orderids, String addr) {

        JSONObject result = new JSONObject();
        result.put("status", "SETTLE_FAIL");

        if (orderids != null && orderids.size() > 0) {
            //更新订单状态
            int changeCount = orderDao.updateOrderState(orderids, OrderBean.STATE_USING);
            //更新订单送货地址
            orderDao.updateOrderAddr(orderids, addr);
            result.put("status", "SUCCESS");
            result.put("COUNT", changeCount);
        }
        return result;
    }

    @Override
    public JSONObject reqRevert(Long orderid) {
        JSONObject result = new JSONObject();
        result.put("status", "REVERT_REQ_FAIL");
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(orderid);
        int changeCount = orderDao.updateOrderState(ids, OrderBean.STATE_APPLYING);
        if (changeCount > 0) result.put("status", "SUCCESS");
        return result;
    }

    @Override
    public JSONObject confirmRevert(Long orderid) {
        JSONObject result = new JSONObject();
        result.put("status", "CONFIRM_REQ_FAIL");
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(orderid);
        int changeCount = orderDao.updateOrderState(ids, OrderBean.STATE_FINISH);
        if (changeCount > 0) result.put("status", "SUCCESS");
        return result;
    }
}
